package com.easygo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easygo.common.PageUtils;
import com.easygo.common.ResultEnum;
import com.easygo.common.ResultResponse;
import com.easygo.pojo.Content;
import com.easygo.pojo.ContentCategory;
import com.easygo.service.ContentCategoryService;
import com.easygo.service.ContentService;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 徐策
 * @date 2020/1/8 12:07
 */
@RestController
@Scope("prototype")
public class ContentCategoryController {
    @Resource
    ContentCategoryService contentCategoryService;

    @Resource
    ContentService contentService;

    //后台广告分页显示的controller
    @RequestMapping("contentCategory_page/{indexPage}/{pageSize}")
    public PageUtils<ContentCategory> getContentByPage(@PathVariable("indexPage")Long indexPage
                                                        ,@PathVariable("pageSize")Long pageSize
                                                        ,@RequestParam(value = "name",defaultValue = "")String name){
        //生成一个ipage实现类 传入indexPage 和 pageSize
        IPage<ContentCategory> iPage = new Page<>(indexPage,pageSize);
        //mybatisPlus 传入iPage和包装后的sql插到一个page
        IPage<ContentCategory> page  =  contentCategoryService.page(iPage,new QueryWrapper<ContentCategory>().eq("del",0).like("name",name));

        Long totalSize = page.getTotal(); //总条数
        System.out.println(totalSize+"==================");
        List<ContentCategory> records = page.getRecords(); //获得分页查询后的列表
        PageUtils<ContentCategory> pageUtils = new PageUtils<>(indexPage,pageSize,totalSize,records);    //封装到pageUtils并返回
        return pageUtils;
    }

    //后台的新增广告分类controller
    @RequestMapping("contentCategory_add")
    public ResultResponse<ContentCategory> contentCategory_add(@RequestBody ContentCategory contentCategory){
        Boolean b = contentCategoryService.save(contentCategory);
        if(b){
                return   ResultResponse.success(ResultEnum.INSERT_SUCCESS);
        }
        return ResultResponse.fail(ResultEnum.INSERT_FAIL);
    }

    //获得所有可用广告分类
    @PostMapping("/contentCategory_list")
    public List<ContentCategory> getcontentCategorys(){
        return contentCategoryService.list(new QueryWrapper<ContentCategory>().eq("del", 0));
    }

    @PostMapping("/contentCategory_updateById")
    public ResultResponse contentCategory_updateById(@RequestBody ContentCategory contentCategory){
        boolean b = contentCategoryService.updateById(contentCategory);
        if(b){
            return ResultResponse.success(ResultEnum.UPDATE_SUCCESS);
        }else{
            return ResultResponse.success(ResultEnum.UPDATE_FAIL);
        }
    }
    @PostMapping("/contentCategory_deleteById/{id}")
    public ResultResponse contentCategory_deleteById(@PathVariable("id") Long id){
        ContentCategory contentCategory = contentCategoryService.getById(id);
        contentCategory.setDel(1L);
        boolean b = contentCategoryService.updateById(contentCategory);
        if(b){
            return ResultResponse.success(ResultEnum.DELETE_SUCCESS);
        }else{
            return ResultResponse.success(ResultEnum.DELETE_FAIL);
        }
    }

    @GetMapping("/getById/{id}")
    public ContentCategory getById(@PathVariable("id") long id){
        return  contentCategoryService.getById(id);
    }

    @GetMapping("/isHasChild/{contentCategoryId}")
    public Integer isHasChild(@PathVariable("contentCategoryId") long contentCategoryId){
        return contentService.list(new QueryWrapper<Content>().eq("category_id",contentCategoryId).eq("del",0)).size();
    }

}
